<?php
declare(strict_types=1);

namespace SixShop\Payment\Hook;

use SixShop\Core\Attribute\Hook;
use SixShop\Payment\PaymentManager;
use think\Collection;

class ConfigHook
{
    public function __construct(private PaymentManager $paymentManager)
    {
    }

    /**
     * 支持小程序环境配置
     */
    #[Hook('after_read_extension_config_payment')]
    public function onConfig(Collection $config): void
    {
        // 支持的小程序环境选项
        $miniProgramEnvironments = [
            [
                "value" => "develop",
                "label" => "开发版"
            ],
            [
                "value" => "trial",
                "label" => "体验版"
            ],
            [
                "value" => "release",
                "label" => "正式版"
            ]
        ];
        $supportedEnvList = [];
        foreach ($this->paymentManager->getAllPayment() as $payment) {
            $supportedEnvList[] = [
                "type" => "checkbox",
                "field" => "supported_env_" . $payment->id,
                "title" => $payment->name,
                "info" => "选择在此插件中支持的小程序环境",
                "options" => $miniProgramEnvironments,
                "\$required" => false,
                "props" => [
                    "_optionType" => 1
                ],
                "_fc_id" => "id_supported_env_checkbox_" . $payment->id,
                "name" => "ref_supported_env_" . $payment->id,
                "display" => true,
                "hidden" => false,
                "_fc_drag_tag" => "checkbox"
            ];
        }
        $config[] =
            [
                'type' => 'elCard',
                'props' => [
                    'header' => '支持的小程序环境'
                ],
                'style' => [
                    'width' => '100%'
                ],
                'children' => $supportedEnvList,
                '_fc_id' => 'id_F40hmie0awi6adc',
                'name' => 'ref_Fps1mie0awi6aec',
                'display' => true,
                'hidden' => false,
                '_fc_drag_tag' => 'elCard'
            ];
    }
}